<?php

/*
 * Copyright 2008 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#namespace tehframework\logging;

#use \Exception;

class Logger
{
	/**
	 * @var array<Log>
	 */
	private $logs = array();
	
	/**
	 * @var array<LogWriter>
	 */
	private $writers = array();
	
	/**
	 * @var bool
	 */
	private $writeOnDestruct;
	
	/**
	 * @param  bool $writeOnDestruct
	 * @param  LogWriter? $writer
	 */
	public function __construct($writeOnDestruct = true, LogWriter $writer = null)
	{
		$this->writeOnDestruct = (bool)$writeOnDestruct;
		
		if ($writer)
		{
			$this->addWriter($writer);
		}
	}
	
	public function __destruct()
	{
		if ($this->writeOnDestruct)
		{
			$this->writeAll();
		}
	}
	
	/**
	 * @return string
	 */
	public function __toString()
	{
		$logs = '';
		
		foreach ($this->logs as $log)
		{
			$logs .= PHP_EOL . $log;
		}
		
		return $logs;
	}
	
	/**
	 * @param  LogWriter $writer
	 * @return Logger
	 */
	public function addWriter(LogWriter $writer)
	{
		$this->writers[] = $writer;
		
		return $this;
	}
	
	/**
	 * @param  Log $log
	 * @return Logger
	 */
	public function log(Log $log)
	{
		$this->logs[] = $log;
		
		return $this;
	}
	
	/**
	 * @param  string $message
	 * @param  string $...
	 * @return Logger
	 */
	public function info($message)
	{
		$args = func_get_args();
		
		$this->addLog(__FUNCTION__, $args);
		
		return $this;
	}
	
	/**
	 * @param  string $message
	 * @param  string $...
	 * @return Logger
	 */
	public function debug($message)
	{
		$args = func_get_args();
		
		$this->addLog(__FUNCTION__, $args);
		
		return $this;
	}
	
	/**
	 * @param  string|Exception $messageOrException
	 * @param  string $...
	 * @return Logger
	 */
	public function error($messageOrException)
	{
		$args = $messageOrException instanceof Exception
			? array($messageOrException->getMessage())
			: func_get_args();
		
		$this->addLog(__FUNCTION__, $args);
		
		return $this;
	}
	
	/**
	 * @return Logger
	 * @throws BadMethodCallException
	 */
	public function writeLast()
	{
		if (empty($this->logs))
		{
			throw new BadMethodCallException(
				'Can not write the last log, because there is not any log at all.'
			);
		}
		
		$this->writeLog(array_pop($this->logs));
		
		return $this;
	}
	
	/**
	 * @return Logger
	 */
	public function writeAll()
	{
		if (!empty($this->logs))
		{
			$this->writeAllLogs($this->logs);
			
			$this->logs = array();
		}
		
		return $this;
	}
	
	/**
	 * @param  Log $log
	 */
	private function writeLog(Log $log)
	{
		foreach ($this->writers as $writer)
		{
			$writer->writeLog($log);
		}
	}
	
	/**
	 * @param  array<Log> $log
	 */
	private function writeAllLogs(array $logs)
	{
		foreach ($this->writers as $writer)
		{
			$writer->writeAllLogs($logs);
		}
	}
	
	/**
	 * @param  string $severity
	 * @param  array<string> $messageAndArgs
	 */
	private function addLog($severity, array $messageAndArgs)
	{
		$this->log(new Log(
			call_user_func_array('sprintf', $messageAndArgs), time(), $severity)
		);
	}
}